package www.wen.com.common.lib.extension.glide

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions


/**
 * @author WEN
 * @Description:
 * @date 2020/11/26 23:11
 */

@SuppressLint("CheckResult")
@BindingAdapter(
        value = ["imageUrl", "placeholder", "error", "fallback", "loadWidth", "loadHeight", "isCircle", "diskCacheStrategy", "withCrossFade"],
        requireAll = false
)
fun setImageUrl(
        view: ImageView,
        source: Any? = null,
        placeholder: Drawable? = null,
        error: Drawable? = null,
        fallback: Drawable? = null,
        width: Int? = -1,
        height: Int? = -1,
        isCircle: Boolean = false,
        diskCacheStrategy: DiskCacheStrategy? = null,
        withCrossFade: Boolean? = true
) {
    // 计算位图尺寸，如果位图尺寸固定，加载固定大小尺寸的图片，如果位图未设置尺寸，那就加载原图，Glide加载原图时，override参数设置 -1 即可。
    val widthSize = (if ((width ?: 0) > 0) width else view.width) ?: -1
    val heightSize = (if ((height ?: 0) > 0) height else view.height) ?: -1
    // 根据定义的 cacheEnable 参数来决定是否缓存
//    val diskCacheStrategy = if (cacheEnable == true) DiskCacheStrategy.AUTOMATIC else DiskCacheStrategy.NONE
    // 设置编码格式，在Android 11(R)上面使用高清无损压缩格式 WEBP_LOSSLESS ， Android 11 以下使用PNG格式，PNG格式时会忽略设置的 quality 参数。
//    val encodeFormat = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) Bitmap.CompressFormat.WEBP_LOSSLESS else Bitmap.CompressFormat.PNG
    val encodeFormat = Bitmap.CompressFormat.PNG

    var diskCacheStrategy1: DiskCacheStrategy? = null
    if (diskCacheStrategy == null) {
        diskCacheStrategy1 = DiskCacheStrategy.AUTOMATIC
    } else {
        diskCacheStrategy1 = diskCacheStrategy
    }

    val requestBuilder: RequestBuilder<Drawable> = Glide.with(view.context)
            .asDrawable()
            .load(source)
            .placeholder(placeholder)
            .error(error)
            .fallback(fallback)
//            .thumbnail(0.33f)
            .override(widthSize, heightSize)
            .skipMemoryCache(false)
            .format(DecodeFormat.PREFER_ARGB_8888)
            .encodeFormat(encodeFormat)
            .encodeQuality(100)
            .diskCacheStrategy(diskCacheStrategy1!!)

    if (isCircle) {
        requestBuilder.circleCrop()
    }

    if (withCrossFade != null && withCrossFade) {
        requestBuilder.transition(DrawableTransitionOptions.withCrossFade())
    }
    requestBuilder.into(view)

}